package us.sunrisemorning.pixivbrower.web.controller;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;

import us.sunrisemorning.pixivbrower.spider.DbQueue;
import us.sunrisemorning.pixivbrower.spider.SpiderService;
import us.sunrisemorning.pixivbrower.spider.SpiderStatus;

public class SearchController extends Controller {
    
    @Inject
    private SpiderService spiderService;
    @Inject
    private DbQueue queue;

    public void index() {
        List<String> queueInfo = spiderService.getQueueInformation();
        SpiderStatus status = spiderService.getStatus();
        
        setAttr("queueInfo", queueInfo);
        setAttr("status", status);
        setAttr("cacheSize", queue.getSize());
        renderTemplate("search.html");
    }

    public void start() {
        String keyword = getPara("keyword");

        spiderService.addKeyword(keyword);

        redirect("/search");
    }
    
    public void stop() {
        spiderService.stop();
        
        redirect("/search");
    }
    
    public void delete() {
        int id = getParaToInt("id");
        
        spiderService.removeKeyword(id);
        
        redirect("/search");
    }
}
